package com.klikmee.sms.core.web.usecases;

import java.util.ArrayList;
import java.util.List;

import com.dotexe.webframework.core.ClientApplication;
import com.dotexe.webframework.core.clientactions.MessageBox;
import com.dotexe.webframework.core.datasources.CollectionDataSource;
import com.dotexe.webframework.core.datasources.ObjectDataSource;
import com.dotexe.webframework.core.triggers.Trigger;
import com.klikmee.sms.core.base.dao.ServiceDAO;
import com.klikmee.sms.core.base.util.SpringBeansLookup;
import com.klikmee.sms.core.model.Client;
import com.klikmee.sms.core.model.Service;
import com.klikmee.sms.core.web.SmsCoreSession;
import com.klikmee.sms.core.web.reports.ReportingService;
import com.klikmee.sms.core.web.reports.dto.MonthlyIncomeItem;
import com.klikmee.sms.core.web.reports.dto.SearchItem;

public class MainForm extends ClientApplication {

	private ObjectDataSource		dsSearchItem;
	private ArrayList<String>		bcSearchItem;
	private CollectionDataSource	dsServices;
	private ArrayList<String>		bcServices;
	private CollectionDataSource	dsResults;
	private ArrayList<String>		bcResults;

	private ServiceDAO				serviceDAO;
	private List<Service>			serviceList;
	private ReportingService		reportingService	= null;

	@Override
	public void load() throws Exception {
		super.load();
		this.dsSearchItem = new ObjectDataSource( "dsSearchItem" );
		this.getDataSources().add( dsSearchItem );
		this.dsServices = new CollectionDataSource( "dsServices" );
		this.getDataSources().add( dsServices );
		this.dsResults = new CollectionDataSource( "dsResults" );
		this.getDataSources().add( dsResults );

		this.triggers.add( new Trigger( this, "btnSearchClick" ) );
		this.triggers.add( new Trigger( this, "btnLogoutClick" ) );

		this.bcSearchItem = new ArrayList<String>();
		this.bcSearchItem.add( "DateFrom" );
		this.bcSearchItem.add( "DateTo" );

		this.bcServices = new ArrayList<String>();
		this.bcServices.add( "Name" );

		this.bcResults = new ArrayList<String>();
		this.bcResults.add( "Month" );
		this.bcResults.add( "VipIncome" );
		this.bcResults.add( "MtsIncome" );
		this.bcResults.add( "TelenorIncome" );
	}

	@Override
	public void start() throws Exception {
		super.start();

		SearchItem searchItem = new SearchItem();
		// set SearchItem properties to current month
		searchItem.setDateFrom( ReportingService.getFirstDayOfTheMonth().getTime() );
		searchItem.setDateTo( ReportingService.getLastDayOfTheMonth().getTime() );

		this.dsSearchItem.setSource( searchItem );
		this.dsSearchItem.bind( bcSearchItem );

		serviceDAO = SpringBeansLookup.getBean( ServiceDAO.class );
		serviceList = serviceDAO.findByClient( SmsCoreSession.getUser() );
		this.dsServices.setSource( serviceList );
		this.dsServices.bind( bcServices );

	}

	public void btnSearchClick() throws InstantiationException, IllegalAccessException, Exception {

		this.reportingService = SpringBeansLookup.getBean( ReportingService.class );
		Client client = SmsCoreSession.getUser();
		
		List<MonthlyIncomeItem> incomeByTimeIntervalAndService = this.reportingService.findMonthlyIncomeByTimeIntervalAndService( ((SearchItem) dsSearchItem.getSource()).getDateFrom(), ((SearchItem) dsSearchItem.getSource()).getDateTo(),
				((Service) dsServices.getSelectedItem()));
		 this.dsResults.setSource(incomeByTimeIntervalAndService);
		 this.dsResults.bind(bcServices);

	}

	public void btnLogoutClick() throws InstantiationException,
			IllegalAccessException, Exception {
		LoginForm login = this.createChildApplication( LoginForm.class );
		this.showChildApp( login );
		login.start();
		this.hide();
	}
}